//
//  XJGoodsAllEvaluateViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/10.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  全部评论

#import "XJGoodsAllEvaluateViewController.h"
#import "XJGoodsAllEvaluateCell.h"


@interface XJGoodsAllEvaluateViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tv;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XJGoodsAllEvaluateViewController

#pragma mark - 一 lazy
- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tv.delegate =self;
        _tv.dataSource  = self;
        
       NSString *clsName = NSStringFromClass([XJGoodsAllEvaluateCell class]);
        
        //应为cell 的高度是在model动态给出来的。如果不先预设一个高度就会导致:闪 + 不滚动就不显示图片。
        _tv.estimatedRowHeight = 200; //默认cell200
        
        [_tv registerNib:[UINib nibWithNibName:clsName bundle:nil] forCellReuseIdentifier:clsName];
        _tv.tableFooterView = [UIView new];
    }
    
    return _tv;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



#pragma mark - 二 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tv.frame = CGRectMake(0, 0, KWidth, KHeight - kStatusBarAndNavigationBarHeight);
    [self.view addSubview:self.tv];
    
    self.navigationItem.title = self.navTitle;
    
    [self requestDataWithGoodsID:self.goodID];
    
}

#pragma mark -  UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *clsName = NSStringFromClass([XJGoodsAllEvaluateCell class]);

    XJGoodsAllEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:clsName];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.evaluateModel = self.dataArray[indexPath.row];
    
    return cell;
}


#pragma mark -  UITableViewDelegate

/**这个高度是要动态的计算的的
 1.如果有图片就是200
 2.如果没有图片就去掉图片的高度。
 最好是将这部分逻辑放到model中进行处理
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 200;
    XJGoodsEvaluateModel *model = self.dataArray[indexPath.row];
    
    return model.height;
}
- (void)requestDataWithGoodsID:(NSString *)goodID {
    
    NSString *gid = [NSString stringWithFormat:@"%@",goodID];
    if([NSString isEmptyString:gid]) return;
    
    [XJHttpTool requestGoodsAllEvaluateWithGoodsId:goodID successH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSArray *goods_comments = responseObjc[@"data"][@"goods_comments"];
            
            for (NSDictionary *dic in goods_comments) {
                XJGoodsEvaluateModel *model = [XJGoodsEvaluateModel mj_objectWithKeyValues:dic];
                
                [self.dataArray addObject:model];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tv reloadData];
        });
        
    } failH:^(NSError *error) {
    
        [JRToast showWithText:[error localizedDescription]];
    }];
}

#pragma mark - 请求数据
/**
 {
 "code": 0,
 "data": {
 "comment_count": 1,
 "goods_comments": [{
 "id": 35,
 "pid": 0,
 "order_id": 237,
 "shop_id": 12,
 "goods_id": 110,
 "option_id": 0,
 "customer_id": 40,
 "comment_state": 1,
 "content": "凉了凉了不错federal",
 "images": "http://static.zertone1.com/upload/avatar/2018_03_23/1ff33bd8ecbf410ca333e79669f5094b.jpg",
 "create_time": 1528167922108,
 "is_delete": 0,
 "active": 1,
 "customer_name": "突突突",
 "customer_avatar": "http://static.zertone1.com/upload/avatar/2018_03_23/b7837afb3d694f39ae39dd5438c56500.png"
 }]
 }
 }
 */


@end
